package cgl.narada.test;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Properties;
import java.util.StringTokenizer;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;

import cgl.narada.jms.NBJmsInitializer;

public class StressJmsConnections implements MessageListener {
  private String hostname;
  private int portnum;
  private String transportType;
  
  private String moduleName = "StressJmsConnections: ";
  
  //public StressJmsConnections() {
 
  //}

  public StressJmsConnections(String hostname, int portnum, 
			     String transportType) {
    this.hostname = hostname;
    this.portnum = portnum;
    this.transportType = transportType;
  }

  public void openAndCloseConnections(int numOfTimes) 
    throws JMSException {
    
    for (int i=0; i < numOfTimes; i++) {
      System.out.println("Connection ["+ i + "] being opened and then closed");
      openAndCloseConnection();
    }

  }


  public void openAndCloseConnection() throws JMSException {
	 Properties props = new Properties();
	 /** These properties pertain to setting up a TCP link */
	 props.put("hostname", hostname); 
	 props.put("portnum", ""+portnum);
	    
    NBJmsInitializer ini = new NBJmsInitializer(props, transportType);
    /* Lookup a JMS connection factory*/
    TopicConnectionFactory conFactory =
      (TopicConnectionFactory)ini.lookup();
    
    /* Create a JMS connection */
    TopicConnection connection =
      conFactory.createTopicConnection("guest","password");

    /**Create a topic session */
    TopicSession subSession =
      connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
   
    /** Create a JMS Topic */
    Topic  jmsTopic = subSession.createTopic("/streams/chat/private/Text");

    /** Create a topic subscriber */
    TopicSubscriber subscriber =  subSession.createSubscriber(jmsTopic);
    subscriber.setMessageListener(this);

    connection.start();

    connection.close();
  } 

  
  public void onMessage(Message message) {
    System.out.println(moduleName + "Received message ");
  }


  public static void main(String[] args) {
    String module = "StressJmsConnections.main()->";
    
    if (args.length < 2) {
      System.out.println(module + "Usage java cgl.narada.test.StressJms" +
			 "Connections <host> <port> <transport>");
    }

    String host = args[0];
    int port = Integer.parseInt(args[1]);
    String transport = "niotcp";
    if (args.length == 3) {
      transport = args[2];
    }
    
    System.out.println("\n\nUsage: num <numOfConnections> ---- Init and Close"
		       + " multiple connections");
    
    try {
      StressJmsConnections jmsConn = new StressJmsConnections(host, port, 
							      transport);
      BufferedReader commandLine = 
	new BufferedReader(new InputStreamReader(System.in));
      while(true){
	String s = commandLine.readLine();
	if(s.equalsIgnoreCase("exit")){
	  break;/* exit program */
	}
	
	if (s.startsWith("num")) {
	  StringTokenizer tok = new StringTokenizer(s, " ");
	  if (tok.countTokens() != 2) {
	    System.out.println("num <numOfConnections> ---- Init and Close" +
			       " multiple connections");
	    continue;
	  } /** check tokens */
	  tok.nextToken();
	  int numOfConnections = Integer.parseInt(tok.nextToken().trim());
	  jmsConn.openAndCloseConnections(numOfConnections);
	  
	}/** if startsWith -> num */
      } /* end while(true) */
    } catch (Exception e) {
      System.out.println(module + e);
    }

    System.out.println(module + "Program closing down ... ");
  }
}

